{% extends 'root_jquery.html' %}
{% load url from future %}
{% block header_scripts %}
<script type="text/javascript"> 
    {# album_pages.html template could be loaded through ajax so declare the functions here#}
    function update_album_pages(url){
        $.get(url, function(data){
	         $('#page_container').html(data);
             ajaxify_links();
	    });
    }
    
    {# next functions cause the album pages to load via ajax if javascript is enabled #}
    function ajaxify_links(){
        var anchor = $('#album_prev_page_link');
        anchor.attr('href',"javascript: update_album_pages(\""+anchor.attr('href')+"\");");
        anchor = $('#album_next_page_link');
        anchor.attr('href',"javascript: update_album_pages(\""+anchor.attr('href')+"\");");
        $('a.album_remove_page_link').each(function(index){
            $(this).attr('href',"javascript: update_album_pages(\""+$(this).attr('href')+"\");");
        });
    }
    $(document).ready(function(){
        ajaxify_links();
    });
    
    function order() {
        document.orderForm.submit();
    }
</script>
{% endblock %}

{% block root_content %}
{% block album_before %}
{% endblock %}
{% if user.is_authenticated and user.is_active and user == album.owner %}
        <h1><a href="{% url 'show_album' album.id %}">{{album.name}}</a></h1>
{% else %}
    <h1><a href="{% url 'show_album' album.id %}?public_token={{album.public_token}}">{{album.name}}</a></h1>
{% endif %}
<p>{{album.description}}</p>

{% if user.is_authenticated and user.is_active and user == album.owner %}
	<div class="album_nav">
	    <div style="visibility: hidden;">
		<form name="orderForm" method="POST" action="{% url 'order_album' album.id pages.number%}">
		   {% csrf_token %}
		</form>
	    </div>
	    <ul>
        {% if user.social_auth.get %}
            <li><a href="{% url 'post_to_facebook' album.id %}">Post album to
            Facebook</a></li>
        {% endif %}
	    <li><a href="javascript: order();">Order a hard copy!</a></li>
	    <li><a href="{% url 'add_page' album.id %}">Add a new page</a></li>
	    {% if public_token_link %}
	        <li><a href="{% url 'remove_public_link' album.id %}">Remove public link </a></li>
	    {% else %}
		    <li>
		       <a href="{% url 'create_public_link' album.id %}">Create a public link</a>
		    </li>
	    {% endif %}
	    </ul>
	</div>
	{% if public_token_link %}
	<div class="public_link">
        <p>Public link: <a
        href="{{public_token_link}}">{{public_token_link}}</a><p></div>
	{% endif %}
{% endif %}

<div class="album_content">
	<div id="page_container" class="album_pages">
	{% block album_pages %}
	    {% include 'album_pages.html' %}
	{% endblock %}
	</div>
</div>
{% endblock %}
